package basic.socket.chat;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * @author bingo
 * @Description
 * @Date 2018/7/20
 */
public class Send implements  Runnable {


    private BufferedReader bufferedReader;

    private DataOutputStream dos;

    private String msg;

    private  boolean isRunning;



    public Send(Socket socket,String name) {
        isRunning = true;
        bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        try {
            dos =  new DataOutputStream(socket.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        send(name);
    }


    public String writeLine(){
        try {
            msg  = bufferedReader.readLine();
        } catch (IOException e) {
            isRunning = false;
            e.printStackTrace();
        }
        return  msg;
    }

    public void send(String message){
        try {

                dos.writeUTF(message);
                dos.flush();
        }catch (IOException e){
            isRunning = false;
          e.printStackTrace();
        }

    }

    @Override
    public void run() {
        while (isRunning){
            writeLine();
            send(msg);
        }

    }
}
